﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Alos.Entity;
using Alos.IRepository;
using PetaPoco;

namespace Alos.Repository
{
    class ReExamSubmitRepository : BaseRepository<ReExamSubmit>, IReExamSubmitRepository
    {
        /// <summary>
        /// 学生课堂测验提交表
        /// </summary>
        /// <param name="ReExamSubmit">学生课堂测验提交</param>
        /// <returns></returns>
        public bool CreatReExamSubmit(ReExamSubmit ReExamSubmit)
        {
            return Convert.ToInt32(ReExamSubmit.Insert()) > 0 ? true : false;
        }
        /// <summary>
        /// 统计学生课堂测验提交
        /// </summary>
        /// <param name="ExamID">测验编码</param>       
        /// <returns></returns>
        public dynamic GetReExamSubmitByExamID(string ExamID)
        {
            var stem = DBContext.Query<dynamic>("  select * from ReTestStem where id in(select StemID from ReExamDetail where ExamID=@0)", ExamID).ToList();
           var ReExamSubmits = DBContext.Query<dynamic>("select * from ReExamSubmit where ExamID=@0", ExamID).ToList();
           var ReExamSubmitDetail = DBContext.Query<dynamic>("select * from ReExamDetail where ExamID=@0", ExamID).ToList();
           foreach (var st in stem)
           {
               st.Detail = ReExamSubmitDetail.FindAll(opt => opt.StemID == st.ID);
           }
           foreach (var stm in ReExamSubmitDetail)
            {
                stm.opts = ReExamSubmits.FindAll(opt => opt.StudentID == stm.StudentID);
            }
           return stem;
        }
        /// <summary>
        /// 修改学生课堂测验提交表
        /// </summary>
        /// <param name="StudentID">学生ID</param>
        /// <param name="ExamID">课堂测验ID</param>
        /// <returns></returns>
        public bool UpdateReExamSubmit(int StudentID, string ExamID)
        {
            var sql = Sql.Builder.Append(@"UPDATE ReExamSubmit SET IsSubmit=1 WHERE ExamID=@0  and StudentID=@1", ExamID, StudentID);
            return DBContext.Execute(sql) > 0 ? true : false;   
        }

        /// <summary>
        /// 添加课堂测验入库记录（课堂测验提交表）
        /// </summary>
        /// <param name="res"></param>
        /// <returns></returns>
        public bool Add(ReExamSubmit res)
        {
            return Convert.ToInt32(res.Insert()) > 0;
        }
    }
}
